package com.study.lilhan.niuke.demo;

import java.util.ArrayList;

/**
 * @Author: lihan
 * @Date : 2021/12/14- 12
 * @DESCRIPTION: com.study.lilhan.niuke.demo
 * @version: 1.0
 */
public class My15 {
    public static void main(String[] args) {

        TreeNode node3 = new TreeNode(3);
        TreeNode node9 = new TreeNode(9);
        TreeNode node20 = new TreeNode(20);
        TreeNode node15 = new TreeNode(15);
        TreeNode node7 = new TreeNode(7);

        node3.left = node9;
        node3.right = node20;

        node20.left = node15;
        node20.right = node7;
        ArrayList<ArrayList<Integer>> arrayLists = levelOrder(node3);
        System.out.println(arrayLists);


    }
    static public ArrayList<ArrayList<Integer>> levelOrder (TreeNode root) {
        ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
        if(root == null){
            return result;
        }


        ArrayList<TreeNode>  nodes = new ArrayList<TreeNode>();
        nodes.add(root);
        while (!nodes.isEmpty()){
            ArrayList<Integer>  tempList = new ArrayList<Integer>();
            ArrayList<TreeNode>  tempNodes = new ArrayList<TreeNode>();
            for(TreeNode node :nodes){
                tempList.add(node.val);
                if(node.left != null){
                    tempNodes.add(node.left);
                }
                if(node.right != null){
                    tempNodes.add(node.right);
                }


            }

            result.add(tempList);
            nodes = tempNodes;
        }


        return  result;
    }
    /*static public ArrayList<Integer> printVal(){

    }*/
}
class TreeNode {
  int val = 0;
    TreeNode left = null;
    TreeNode right = null;

    public TreeNode(int val) {
        this.val = val;
    }
}